package com.leyou.goods.listener;

import com.leyou.goods.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GoodsWebRabbitMQListener {

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "LEYOU.CREATED.GOODSWEB.QUEUE",durable = "true"),
                    exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
                    key = {"item.insert","item.update","item.saleable"}
            )
    )

    //保存静态页面
    public void save(Long id){
        if(id==null){
            return;
        }
        goodsHtmlService.createStaticHtml(id);
    }



    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "LEYOU.DELETE.GOODSWEB.QUEUE",durable = "true"),
                    exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
                    key = {"item.delete"}
            )
    )
    //监听是否创建或覆盖掉静态页面
    public void delete(Long id){
        if(id==null){
            return;
        }
        goodsHtmlService.delete(id);
    }

}
